<?

/** dzkone fetch functionality.
 *
 *  This collection of functions (libary) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author: JQ + Henri Ranki 11.6.2001
 *  $Id: dzkone_fetch.php,v 1.5 2005/11/01 12:59:05 jyry Exp $
 */

/** Performs fetch operation for all requested tables.
 *  Fetch expects fetch declaration in DZK_DETAILS control property.
 *  Fetch declaration are stored in ini file format:
 *  [any_fetch_name] table=tablename keycols=col1:int,col2,col3:date
 *  keyvals=urlencoded_array_using_SO_URL_ARRAY_DELIM_as_delimiter
 *  datacols=col_a:int/local_col_name_a,col_b/local_col_name_b
 *
 *  Each column may or may not have ":datatype" notation, but those left
 *  without one are considered as ":text". Columns may have two names:
 *  source table name and local name. That is "read from 'source' and store
 *  it to 'local'. The notation is "source_name:datatype/local_name". Local
 *  does not and should not contain datatype.
 *
 *  @param dzKone $Kone dzKone object to operate with.
 *  @return bool Returns true(1) on success, otherwise false(0).
 */
function __dzkone__fetch(&$Kone) {
    $item = $Kone->__InItu->getBlockItemAt(DZI_CONTROL, DZK_DETAILS);
    $item = str_replace(" ", "\n", $item);
    $ini = new soIni($item);
    $i = 0;
    $result = new soSet();
    $icount = $ini->getSectionCount();
    $cache = &new soArraySet();
    for ($i = 0; $i < $icount; $i++) {
        $key_cols = $ini->getValue($i, "keycols", SOINI_NOSUB);
        $key_vals = $ini->getValue($i, "keyvals", SOINI_NOSUB);
        $key_vals = soUrlDecodeArray($key_vals);

        $dcols = str_replace("+", " ",
                             $ini->getValue($i, "datacols", SOINI_NOSUB));
        $result = __dzkone__fetch_sub($Kone->__Soda,
                                     $ini->getValue($i, "table", SOINI_NOSUB),
                                     $key_cols, $key_vals, $dcols, $cache);
        $rcols = $result->getKeys();
        $rcount = count($rcols);
        for ($r = 0; $r < $rcount; $r++) {
            $rcol = soExtractVal($rcols[$r], ":", 1);
            $Kone->__Purkki->setStoreCol(TAFFEL, $rcol."#",
                                         $result->getItem($rcol));
        }; // for
    }; // while

    /* control result is set to no. columns returned */
    $Kone->__OutItu->setBlockItem(DZI_CONTROL, DZK_RESULT, $rcount);
    return 1;
}; // __dzkone__fetch


/** Performs a actual fetch call(s).
 *  Used by __fetch and readSet (for pseudo columns).
 *  @param dzKone $Soda Database access object.
 *  @param string $KeyCols Comma delimited list of key columns
 *  @param array/any $KeyVals Key values as an array
 *  @param string $SourceTable Source table to make fetch on.
 *  @param string $DataCols Space delimited list of columns to fetch. Each name
 *      may contain result column name separated by slash: "name/local".
 *      This means "read from 'name' but call is 'local' in the result
 *      set." Result column name may not contain datatype, but table column
 *      name may just fine: "name:int/local".
 *  @param soArraySet $Cache When not NULL, is used to check-up whether a
 *      fetch value is found in it. Initialised by passing an empty soArraySet
 *      to the function.
 *  @return soSet Returns set of columns and a value for each as an array.
 */
function __dzkone__fetch_sub(&$Soda, $SourceTable, $KeyCols, $KeyVals,
                             $DataCols, &$Cache) {
    include("sodebug.inc");

    soDebug("__dzkone__fetch_sub entered", SOD_DET);
    $key_cols = soExplode($KeyCols, "+");
    $result = new soSet();

    /* Split return columns names to source and local cols names */
    $s_cols = soExplode($DataCols, " ");
    $l_cols = $s_cols;
    $scount = count($s_cols);
    for ($s = 0; $s < $scount; $s++) {
        $split = soSplitStr($s_cols[$s], "/", 1);
        if (count($split)) {
            $s_cols = $split[0];
            $l_cols = $split[1];
        }; // if count
    }; // for $s

    /* make "key=value,key2=value2" array for quick fetch */
    $keys = array();
    $icount = count($key_cols);
    for ($i = 0; $i < $icount; $i++) {
        $keys[] = $key_cols[$i]."=".$KeyVals[$i];
    }; // for $i
    $cache_key = $SourceTable."\r".
                 implode("\t", $keys)."\r".
                 implode("\t", $s_cols);
    $result = &$Cache->getItem($cache_key);
    if ($Cache->Error) {
        $values = soQuickDbFetch($SourceTable, $keys, $s_cols, $Soda);
        $result = new soArraySet();
        /* Rename result column, is necessary */
        for ($s = 0; $s < $scount; $s++) {
            $l_col = soExtractVal($l_cols[$s], ":", 1);
            $result->setItem($l_col, array($values[$s]));
        }; // for $s
        $Cache->setItem($cache_key, $result);
    };

    return $result;
}; // __dzkone__fetch_sub


?>
